package com.tencent.wxcloudrun.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tencent.wxcloudrun.dao.PostMapper;
import com.tencent.wxcloudrun.model.MaryPost;
import com.tencent.wxcloudrun.model.Post;
import com.tencent.wxcloudrun.service.PostService;
import com.tencent.wxcloudrun.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lichongbing
 * @version 1.0.0
 * @createdate 2023/2/17 01:25
 * @description: TODO
 */
@Service
public class PostServiceImpl implements PostService {
    @Autowired
    private PostMapper postMapper;

    @Override
    public List<Post> list(int per_page,int page ) {
        Map<String, Object> data = new HashMap<>();
        data.put("currIndex", (page-1)*per_page);
        data.put("pageSize", per_page);
        return postMapper.list(data);
    }

    @Override
    public Post getPostdetals(int id) {
        return postMapper.getPostdetals(id);
    }

    @Override
    public int updateAvatarUrl(String avatarUrl, int postid) {
        String avatarUrls =   postMapper.getAvatarUrls(postid);
        JSONObject jsonObject = new JSONObject();
        if(avatarUrls == null){
            JSONArray objects = new JSONArray();
            jsonObject.put("avatarUrl", avatarUrl);
            objects.add(jsonObject);
            String s = objects.toString();
            int i = postMapper.updateAvatarUrl(s, postid);
            return i;
        }else {
            JSONArray objects = JSON.parseArray(avatarUrls);
            jsonObject.put("avatarUrl", avatarUrl);
            JSONArray objects1 = new JSONArray();
             objects1.add(jsonObject);
            for (int i = 0; i < objects.size(); i++) {
                objects1.add(objects.get(i));
            }
            String s = objects1.toString();
            int i = postMapper.updateAvatarUrl(s, postid);
            return i;
        }
    }

    @Override
    public int updatelikeCont(int postid) {
        int like_count = Integer.parseInt(postMapper.getPostdetals(postid).getLike_count());
        like_count = like_count + 1;
        int likeconts =  postMapper.updatelikeCont(String.valueOf(like_count),postid);
        return likeconts;
    }

    @Override
    public List<MaryPost> getMaryPostsByOpenid(int per_page, int page, String openid) {
        Map<String, Object> data = new HashMap<>();
        data.put("currIndex", (page-1)*per_page);
        data.put("pageSize", per_page);
        data.put("openid", openid);
        return postMapper.getMaryPostsByOpenid(data);
    }

    @Override
    public List<Post> listbycategoriesid(int per_page, int page, int categories) {
        Map<String, Object> data = new HashMap<>();
        data.put("currIndex", (page-1)*per_page);
        data.put("pageSize", per_page);
        data.put("categoriesJson", categories);
        return postMapper.listbycategoriesid(data);
    }

    @Override
    public List<Post> listsearchresults(int per_page, int page, String search) {
        Map<String, Object> data = new HashMap<>();
        data.put("currIndex", (page-1)*per_page);
        data.put("pageSize", per_page);
        data.put("search", search);
        return postMapper.listsearchresults(data);
    }

    @Override
    public int addpost(String postTitle, String postContent, String openid) {
        List<Map<String, Object>> addressidByopenid = postMapper.getAddressidByopenid(openid);
        Map<String, Object> objectMap = addressidByopenid.get(0);
        Integer userId = (Integer) objectMap.get("userId");
        System.out.println(userId);
        Integer addressid = (Integer) objectMap.get("addressid");
        String categoryname = (String) objectMap.get("name");
        System.out.println(addressid);
        String date = DateUtils.getEndTime("yyyy-MM-dd'T'HH:mm:ss");
        String post_date = DateUtils.getEndTime("yyyy-MM-dd");
        String addressids = "["+addressid+"]";
        JSONObject postTitlejson = new JSONObject();
        postTitlejson.put("rendered",postTitle);
        postTitle = postTitlejson.toString();
        JSONObject postCoentJson = new JSONObject();
        postCoentJson.put("rendered",postContent);
        postContent = postCoentJson.toString();
        int addpost = postMapper.addpost(postTitle,postContent, addressids,userId,date,categoryname,post_date);
        return addpost;
    }

    @Override
    public List<Post> listbycategoriesidlocal(int per_page, int page, int categories) {
        Map<String, Object> data = new HashMap<>();
        data.put("currIndex", (page-1)*per_page);
        data.put("pageSize", per_page);
        data.put("categoriesJson", categories);
        return postMapper.listbycategoriesidlocal(data);
    }


}
